package com.exmaple.listviewswipeleftdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;


public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.SimpleViewHolder> {

    private Context mContext;
    private List<UserBean> mList;


    private ItemTouchListener mItemTouchListener;

    public void setData(List<UserBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public interface ItemTouchListener {
        void onItemClick(int position);
    }

    public RecyclerAdapter(Context context, List<UserBean> data, ItemTouchListener itemTouchListener) {
        this.mContext = context;
        this.mList = data;
        mItemTouchListener = itemTouchListener;
    }

    public void addData(List<UserBean> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item, parent, false);
        return new SimpleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SimpleViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        final SwipeItemLayout editLayout = holder.editLayout;
        final UserBean historyMedia = mList.get(position);
        holder.tvTitle.setText(historyMedia.getName());


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        holder.vDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemTouchListener != null) {
                    mItemTouchListener.onItemClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        else
            return 0;
    }


    public class SimpleViewHolder extends RecyclerView.ViewHolder {
        SwipeItemLayout editLayout;
        TextView tvTitle;
        View vDelete;

        SimpleViewHolder(final View itemView) {
            super(itemView);
            editLayout = (SwipeItemLayout) itemView.findViewById(R.id.history_layout);
            tvTitle = (TextView) itemView.findViewById(R.id.item_history_tv);
            vDelete = itemView.findViewById(R.id.history_right_menu);
        }
    }

}
